package com.face.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.face.server.entity.Admin;
import com.face.server.entity.Auth;
import com.face.server.entity.DormitoryBuild;
import com.face.server.repository.AdminRepository;
import com.face.server.repository.AuthRepository;
import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.service.AdminService;
import com.face.server.service.UtileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ryan on 2018/1/29.
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private UtileService utileService;

    @Autowired
    private AuthRepository authRepository;

    @Autowired
    private AdminRepository adminRepository;

    @Autowired
    private DormitoryBuildRepository dormitoryBuildRepository;


    @Override
    public Admin addAdmin(String adminName, Boolean adminSex, String adminPhone, String adminIdentity) {
        Admin admin = new Admin();
        admin.setName(adminName);
        admin.setSex(adminSex);
        admin.setPhone(adminPhone);
        admin.setIdentity(adminIdentity);
        admin.setDormitoryBuilds(null);
        admin.setDormitoryNumList(null);
        admin.setRole(null);
        Auth auth = new Auth();
        auth.setUsername(adminPhone);
        auth.setPassword(adminIdentity.substring(12));
        authRepository.save(auth);
        admin.setAuth(auth);
        return adminRepository.save(admin);
    }

    @Override
    public Admin deleteOwnBuilds(String id) {
        Admin admin = adminRepository.findOne(id);
        admin.setDormitoryBuilds(null);
        admin.setDormitoryNumList(null);
        return adminRepository.save(admin);
    }

    @Override
    public Page<Admin> listAdminsInBuildMgt(Pageable pageRequest, String searchKey) {
        if (searchKey==null)
            return adminRepository.findByDormitoryNumListExists(true,pageRequest);
        else if (UtileService.isNum(searchKey))
            return adminRepository.findByNameRegexOrDormitoryNumListIs(pageRequest,searchKey,Integer.parseInt(searchKey));
        return adminRepository.findByNameRegex(searchKey,pageRequest);
    }

    @Override
    public Admin asignBuilds(String id, String searchData) {
        Admin admin = adminRepository.findOne(id);
        List<DormitoryBuild> dormitoryBuilds = new ArrayList<DormitoryBuild>();
        List<Integer> dormitoryNumList = new ArrayList<Integer>();
        JSONObject searchObj = new JSONObject();
        String[] builds=null;
        if (searchData==null||searchData.trim().equals("")||searchData.trim()==null)
            return null;
        builds=searchData.split(",");
        System.out.println(searchData);
        System.out.println(builds[0]);
        Arrays.stream(builds).forEach(e->{
            int buildNum=Integer.parseInt(e.toString());
            DormitoryBuild dormitoryBuild=dormitoryBuildRepository.findByBuildingNum(buildNum);
            if (dormitoryBuild!=null){
                dormitoryBuilds.add(dormitoryBuild);
                dormitoryNumList.add(dormitoryBuild.getBuildingNum());
            }
        });
        admin.setDormitoryNumList(dormitoryNumList);
        admin.setDormitoryBuilds(dormitoryBuilds);
        return adminRepository.save(admin);
    }

    @Override
    public Admin addToBuildMgt(String name) {
        Admin admin = adminRepository.findByName(name);
        List<DormitoryBuild> dormitoryBuilds = new ArrayList<DormitoryBuild>();
        admin.setDormitoryBuilds(dormitoryBuilds);
        List<Integer> dormNum = new ArrayList<Integer>();
        admin.setDormitoryNumList(dormNum);
        return adminRepository.save(admin);
    }

    @Override
    public List<Admin> listAdmins(String searchKey) {
        List<Admin> adminList = new ArrayList<Admin>();
        if (searchKey == null)
            adminList = adminRepository.findAll();
        else
            adminList = adminRepository.findByNameOrPhone(searchKey);
        adminList.stream().forEach(e -> {
            e.setDormitoryNumList(null);
            e.setDormitoryBuilds(null);
            e.setAuth(null);
        });
        return adminList;
    }

    @Override
    public Admin findOne(String id) {
        Admin admin = new Admin();
        admin = adminRepository.findOne(id);
        Auth auth = admin.getAuth();
        auth.setPassword(null);
        auth.setUsername(null);
        admin.setAuth(auth);
        admin.setDormitoryNumList(null);
        admin.setDormitoryBuilds(null);
        return admin;
    }

    @Override
    public Admin editInfo(String id, String adminName, Boolean adminSex, String adminPhone, String adminIdentity) {
        Admin admin = adminRepository.findOne(id);
        admin.setName(adminName);
        admin.setSex(adminSex);
        admin.setPhone(adminPhone);
        admin.setIdentity(adminIdentity);
        Auth auth = admin.getAuth();
        admin.setAuth(auth);
        return adminRepository.save(admin);
    }

    @Override
    public List<Integer> listOwnBuilds(String id) {
        Admin admin = adminRepository.findOne(id);
        return admin.getDormitoryNumList();
    }

    @Override
    public String changePwd(String authAndPwd) {
        JSONObject jsonObject = new JSONObject();
        if(authAndPwd!=null){
            jsonObject = JSONObject.parseObject(authAndPwd);
            String username =(String)jsonObject.get("username");
            String password = (String)jsonObject.get("password");
            String newPwd = (String)jsonObject.get("newPwd");
            if(username==null||password==null||newPwd==null){
                return "用户名,旧密码,新密码都不可为空";
            }
            Admin admin = adminRepository.findByUsernameAndPassword(username,password);
            if(admin!=null){
                Auth auth = new Auth();
                auth.setUsername(username);
                auth.setPassword(newPwd);
                admin.setAuth(auth);
                adminRepository.save(admin);
                return "密码修改成功";
            }else{
                return "用户名或密码错误";
            }
        }else {
            return "请提交用户名和旧密码以及新密码";
        }
    }

    @Override
    public String findPwd(String identityAndPhone) {
        JSONObject jsonObject = new JSONObject();
        if(identityAndPhone!=null){
            jsonObject = JSONObject.parseObject(identityAndPhone);
            String identity =(String)jsonObject.get("identity");
            String phone = (String) jsonObject.get("phone");
            if(identity==null||phone==null){
                return "信息错误";
            }
            Admin admin = adminRepository.findByIdentityAndPhone(identity,phone);
            if(admin == null){
                return "信息错误";
            }else{
                Auth auth = new Auth();
                auth.setUsername(admin.getAuth().getUsername());
                auth.setPassword(admin.getIdentity().substring(12));
                admin.setAuth(auth);
                adminRepository.save(admin);
                return "用户名为手机号,密码已被重置为身份证号后六位";
            }
        }
        return "信息错误!!";
    }

}

